/**
 * 
 */
package org.nightrunner.d3.taglib;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.nightrunner.d3.model.PaginationModel;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * Pagination jsp tag
 * 
 * @author haiyang.chen
 * @date 2012-1-4
 */
public class PaginationTag extends TagSupport {

	private static final long serialVersionUID = 4351872981747941318L;

	@SuppressWarnings("unused")
	private static final String CURRENT_PAGE = "currentPage";

	private PaginationModel<List<Object>> model;

	private String name;

	private String url;

	private String pattern = "currentPage={currentPage}&rowSize={rowSize}";

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the model
	 */
	public PaginationModel<List<Object>> getModel() {
		return model;
	}

	/**
	 * @param model
	 *            the model to set
	 */
	public void setModel(PaginationModel<List<Object>> model) {
		this.model = model;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
	 */
	@Override
	public int doStartTag() throws JspException {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("<div id=\"ecPage\"><table><tbody><tr>");
			JsonArray jsonArray = model.getParamArray();
			StringBuffer param = new StringBuffer();
			if (jsonArray != null) {
				for (JsonElement element : jsonArray) {
					JsonObject tmp = element.getAsJsonObject();
					Set<Entry<String, JsonElement>> tmpSet = tmp.entrySet();
					for (Entry<String, JsonElement> entry : tmpSet) {
						param.append("&");
						param.append(entry.getKey() == null ? "" : entry
								.getKey().trim());
						param.append("=");
						param
								.append(entry.getValue().getAsString() == null ? ""
										: entry.getValue().getAsString().trim());
					}
				}
			}

			if (model.getObject().size() > 0) {
				for (int pageIndex = 0; pageIndex < model.getPageAmount(); pageIndex++) {
					sb.append("<td>");
					if (pageIndex == model.getCurrentPage()) {
						sb.append("<span>");
						sb.append(pageIndex + 1);
						sb.append("</span>");
					} else {
						sb.append("<a href=\"");
						sb.append(url);

						// if (url.contains("?")) {
						// sb.append("&");
						// } else {
						// sb.append("?");
						// }

						sb.append(pattern.replace("{currentPage}",
								Integer.toString(pageIndex)).replace(
								"{rowSize}",
								Integer.toString(model.getRowSize())));

						// sb.append(param);
						sb.append("\" >");

						sb.append(pageIndex + 1);
					}
					sb.append("</td>");
				}
			}

			sb.append("<td>&nbsp;每页显示");
			sb.append(model.getRowSize());
			sb.append("条<td>");
			sb.append("<td>共");
			sb.append(model.getAllRowSize());
			sb.append("条<td>");
			sb.append("<tr><tbody><table></div>");
			this.pageContext.getOut().println(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
}